<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" type="text/javascript"></script>
  <style>

  </style>
</head>

<body>
  <div id='root'>
    <ac></ac>

    <div> ================== </div>
    <bc></bc>
  </div>

  <script>
    // * 创建一个事件总线，新的Vue实例
    const eventBus = new Vue();

    // * 局部注册组件
    let aComponent = {
      data: function () {
        return {
          msg: 'aaaaa'
        }
      },
      template: `
                <div>
                    <div>a组件</div>
                    <div>a的信息 :<input v-model='msg'/></div>
                    <button @click='send'>send</button>
                </div>
            `,
      methods: {
        send() {
          eventBus.$emit('send', this.msg);
        }
      }
    }

    let bComponent = {
      data: function () {
        return {
          text: ''
        }
      },
      template: `
                <div>
                    <div>b组件</div>
                    <div>接收到的参数: {{text}}</div>
                </div>
            `,
      methods: {

      },
      mounted() {
        eventBus.$on('send', (v) => {
          console.log('b组件 ===>', v);

          this.text = v;
        })
      }
    }


    let vm = new Vue({
      el: '#root',
      // * 局部注册组件
      components: {
        ac: aComponent,
        bc: bComponent
      },
      data: function () {
        return {

        }
      },
      methods: {

      },
      computed: {

      }
    })

  </script>
</body>

</html>
